from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

# 注意  测试框架 geckodriver.exe 需要和 脚本文件 在同一文件夹
driver = webdriver.Firefox(executable_path="./geckodriver.exe",
                           service_args=['--marionette-port', '2828', '--connect-existing'])
# # driver.get("https://erp.jianwangkeji.cn/#/home/home")     # 获取地址
driver.implicitly_wait(10)  # 隐试等待 最大等待 10秒

# 一些全局变量
project_section = "广州软件学院"  # 项目标段
# 物料信息
material_name = "片石"  # 需要申请的物料
count = '50'  # 整个流程需要申请的数量以及所有入库、出库数量
location = '广东省广州市从化区广州软件技术学院'  # 默认全部收货地址


# 表单说明 用于调试 运行时要注释
# application_form = "SQ-20240502-8062"  # 物资申请单 fbs
# purchase_form = "CGJH-20240502-8063"  # 采购订单  xmb
# supplier_form = 'GHSQ-20240502-8064'  # 供货申请单 gys
# gys_buy_form = 'RK-20240502-8065'  # 供应商入库单 增加库存 gys
# put_out_form = ''  # 出库单号    # 供应商出库单  gys
# put_in_form = "RK-20240502-8067"  # 项目部 入库单  xmb
# add_fld = 'CKXQ-20240502-8069'  # 项目部 发料单     xmb
# put_in_form = "RK-20240502-8071"  # 分包商入库单    fbs


def xpath(xpath_str):
    try:
        a = WebDriverWait(driver, 5).until(
            EC.presence_of_element_located((By.XPATH, xpath_str)))
        a.click()
        sleep(0.1)
    except TimeoutException:
        print("未在指定时间内找到可点击的元素")
        sleep(0.1)
    except Exception as e:
        # print(f"发生错误: {e}")
        sleep(0.1)


# 定义输入函数 用于定位xpath 然后输入信息
def xpath_input(xpath_str, input):
    try:
        a = WebDriverWait(driver, 5).until(
            EC.presence_of_element_located((By.XPATH, xpath_str)))
        a.send_keys(input)
        sleep(0.1)
    except TimeoutException:
        print("未在指定时间内找到可点击的元素")
        sleep(0.5)
    except Exception as e:
        print(f"发生错误: {e}")
        sleep(0.5)


# 保存单号
def xpath_text(xpath_str):
    sleep(1)
    return driver.find_element_by_xpath(xpath_str).text


# 查找  xpath_str模糊地址 + 名字
def query(xpath_str, text):
    sleep(0.2)
    xpath(xpath_str + '/span[contains(text(),"%s")]' % text)


# 导航搜索定位  3级目录
def navbar(text1, text2, text3):
    sleep(0.5)
    query('/html/body/div/div/section/section/aside/ul/div/*/div', text1)
    query("//*[@id='app']/div/section/section/aside/ul/div/*/ul/div/li/div", text2)
    query('//*[@id="app"]/div/section/section/aside/ul/div/*/ul/div/li/ul/*/li', text3)


# 查询处理单号
def processing_form(form):
    sleep(1)
    # 输入
    xpath_input('//*[@id="app"]/div/section/section/main/section/div/*/form/div[4]/div/div/input', form)
    # 查询
    query('//*[@id="app"]/div/section/section/main/section/div/*/form/*', "查询")
    sleep(1)
    # 处理
    query('//*[@id="app"]/div/section/section/main/section/div/*/*/div/*/table/tbody/tr/*/div/*', "处理")
    # query('//*[@id="app"]/div/section/section/main/section/div/*/*/div/*/table/tbody/tr/*/div/*','')


# 填写物料信息  （物料名字，数量，选择物料，物料名称搜索框，查询，选择，输入数量）
def material_message(name, count, xpath_str1, xpath_str2_name, xpath_str3, xpath_str4, xpath_str5_count):
    sleep(1)
    # 新增加申请单的物料信息
    # 物料名称
    xpath(f'{xpath_str1}')
    # 搜索申请的物料名称
    xpath_input(f'{xpath_str2_name}', name)
    # 查询
    query(f'{xpath_str3}', "查询")
    # 选择
    query(f'{xpath_str4}', "选择")
    # 输入需要申请物料的数量
    xpath_input(f'{xpath_str5_count}', count)


# 查询单号并且 处理-> 确认 （驳回暂时不支持）
def process_an_order(form):
    processing_form(form)
    sleep(2)   # 一次报错
    # 处理物质申请单 确认或者驳回
    query('//button[1]', "确认")
    # query('//*[@id="app"]/div/section/section/main/section/div/*/div/*/div/*/*/*/',"驳回") # 后面有时间可以写分支
    sleep(1)
